﻿using CommonHelper.AutofacCommon;
using Newtonsoft.Json.Linq;
using Order.MicroService.IService;
using Polly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Order.MicroService.Service
{
    public class TestService : ITestService
    {
        [AutowiredProperty]
        private IOrderService _testServiceAProp { get; set; }

        private Policy<string> _policy;

        private readonly IHttpClientFactory _httpClientFactory;

        public TestService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
            //_policy = Policy<string>
            //    .Handle<Exception>()
            //    .Fallback(() => {
            //        return "错误信息";
            //    });
        }

        /// <summary>
        /// 调用AccountService 来获取结果
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetAccountAsync()
        {
            var client = _httpClientFactory.CreateClient("AccountService"); //复用在Startup中定义的client_1的httpclient
            //var client2 = _httpClientFactory.CreateClient(); //新建一个HttpClient
            var result2 = await client.GetAsync("/api/Account/GetAcountInfo");
            Console.WriteLine(result2.Content.ReadAsStringAsync().Result);
            Console.WriteLine(result2.StatusCode.ToString());
            var nicai = result2.Content.ReadAsStreamAsync().Result.ToString();
            var nextValue = result2.Content.ReadAsStringAsync().Result;
            JObject config = JObject.Parse(nextValue);
            var woAini = config["AccountName"].ToString();
            return woAini;            
        }

        public string GetTestA()
        {
            return "woaini";
        }
    }
}
